use lazy_static::lazy_static;
use rbase::db::DbColumnDefine;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Deserialize, Serialize, Default)]
#[sea_orm(table_name = "t_user")]
pub struct Model {
    #[sea_orm(primary_key)]
    pub id: i64,

    #[sea_orm(default_value = "")]
    pub name: Option<String>,

    #[sea_orm(default_value = "")]
    pub login_name: Option<String>,

    #[sea_orm(default_value = "")]
    pub password: Option<String>,

    #[sea_orm(default_value = "")]
    pub salt: Option<String>,

    #[sea_orm(default_value = 0)]
    pub created_at: Option<i64>,

    #[sea_orm(default_value = 0)]
    pub modified_at: Option<i64>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation, Deserialize, Serialize)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {} // 必须实现这个trait

pub type UserEntity = Entity;
pub type UserModel = Model;
pub type UserActiveModel = ActiveModel;

pub static TABLE_NAME: &str = "t_user";
pub static CREATE_QUERY: &str = r#"
    CREATE TABLE IF NOT EXISTS t_user (
        id INTEGER NOT NULL PRIMARY KEY,
        login_name TEXT NOT NULL UNIQUE 
    );
"#;

lazy_static! {
    pub static ref TABLE_COLUMNS: Vec<DbColumnDefine<'static>> = vec![
        DbColumnDefine::new("name", "TEXT", "ALTER TABLE t_user ADD COLUMN name TEXT nullable"),
        DbColumnDefine::new("password", "TEXT", "ALTER TABLE t_user ADD COLUMN password TEXT nullable"),
        DbColumnDefine::new("salt", "TEXT", "ALTER TABLE t_user ADD COLUMN salt TEXT nullable"),
        DbColumnDefine::new("created_at", "INTEGER", "ALTER TABLE t_user ADD COLUMN created_at INTEGER nullable"),
        DbColumnDefine::new("modified_at", "INTEGER", "ALTER TABLE t_user ADD COLUMN modified_at INTEGER nullable"),
    ];
}

pub fn get_column(name: &String) -> Column {
    match name.as_str() {
        "id" => Column::Id,
        "name" => Column::Name,
        "login_name" => Column::LoginName,
        "password" => Column::Password,
        "salt" => Column::Salt,
        "modified_at" => Column::ModifiedAt,
        "created_at" => Column::CreatedAt,
        _ => Column::Id,
    }
}
